//
//  HWWuKongDetailListVC.swift
//  HWanGame
//
//  Created by Apple on 2024/8/16.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import Stevia
import JXSegmentedView

private let headerID    = "HWWuKongDetailListTopView"
class HWWuKongDetailListVC: HWBaseViewController {
    
    private var topView = HWWuKongDetailTopView()
    private var bottomView = HWWuKongDetailBottomView()
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.bounces = false
        tableView.register(HWWuKongDetailGamePicCell.self,HWWuKongDetailGameInfoCell.self,HWWuKongDetailCommitCell.self,HWGameDetailLikeCell.self)
        tableView.register(HWWuKongDetailHeaderView.self, forHeaderFooterViewReuseIdentifier: headerID)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 200*widthScale
        return tableView
    }()
    
    private let sectionTitle = ["游戏图片/视频","游戏简介","玩家评论","猜你喜欢"]
    var httpManager = HWGameDetailHttpManager()
    var topModel: HWGameDetailModel = HWGameDetailModel()
    var detailModel: HWGameDetailInfoModel = HWGameDetailInfoModel()
    var commentList:[HWGameDetailCommentListModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationView.isHidden = true
        view.backgroundColor = .clear
        loadData()
        view.subviews{
            tableView
            bottomView
        }
        
        tableView.leading(0).trailing(0).bottom(.tabBar_height).top(0)
        bottomView.leading(0).trailing(0).bottom(0).height(.tabBar_height)
        topView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 76*widthScale)
        tableView.tableHeaderView = topView
        
        bottomView.clickCollectBlock = {
            if self.topModel.shoucang == 0{
                self.httpManager.setGameDetailCollection(id: "584",type:"0") { success in
                    self.httpManager.getGameDetailTop(gameId: "584") { model in
                        self.topModel.shoucang = model.shoucang
                        self.topModel.shoucang_id = model.shoucang_id
                        self.bottomView.model = self.topModel
                    } failure: { errorString in
                    }
                } failure: { errorString in
                    self.showHUD(title: errorString)
                }
            }else {
                self.httpManager.setGameDetailCollectionDel(id: self.topModel.shoucang_id,type:"0") { success in
                    self.httpManager.getGameDetailTop(gameId: "584") { model in
                        self.topModel.shoucang = model.shoucang
                        self.topModel.shoucang_id = model.shoucang_id
                        self.bottomView.model = self.topModel
                    } failure: { errorString in
                    }
                } failure: { errorString in
                    self.showHUD(title: errorString)
                }
            }
        }

        bottomView.clickCommitBlock = {
            
            let commentRelease = HWGameCommentReleaseController()
            commentRelease.gameId = "584"
            commentRelease.releaseCallBack = {
                self.loadData()
            }
            self.navigationController?.pushViewController(commentRelease, animated: true)
        }
    }
    

}

//MARK: - UITableViewDelegate, UITableViewDataSource
extension HWWuKongDetailListVC: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        sectionTitle.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if section == 2 {
            return self.detailModel.commentList.count
        }
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "HWWuKongDetailGamePicCell", for: indexPath) as! HWWuKongDetailGamePicCell
            cell.gameModel = self.topModel
            cell.imageModel = self.detailModel
            return cell
        }else if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "HWWuKongDetailGameInfoCell", for: indexPath) as! HWWuKongDetailGameInfoCell
            cell.model = self.detailModel
            cell.showMoreInfo = {
                tableView.reloadData()
            }
            return cell
        }else if indexPath.section == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "HWWuKongDetailCommitCell", for: indexPath) as! HWWuKongDetailCommitCell
            cell.model = self.detailModel.commentList[indexPath.row]
            cell.clickLikeBlock = { id in
                self.httpManager.getGameDetailCommentZan(id: id) { success in
                    self.httpManager.getGameDetailCommentList(gameId: "584") { models in
                        self.detailModel.commentList = models
                        tableView.reloadData()
                    } failure: { errorString in
                        
                    }
                } failure: { errorString in
                    
                }
            }            
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "HWGameDetailLikeCell", for: indexPath) as! HWGameDetailLikeCell
            cell.model = self.detailModel
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if indexPath.section == 2 {
            let model = self.detailModel.commentList[indexPath.row]
            let sonController = HWGameCommentSonController()
            sonController.id = model.id
            sonController.gameId = "584"
            sonController.releaseCallBack = {
                self.loadData()
            }
            self.navigationController?.pushViewController(sonController, animated: true)
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerID) as! HWWuKongDetailHeaderView
        header.titleStr = sectionTitle[section]
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 35*widthScale
    }
}

extension HWWuKongDetailListVC {
    func loadData(){
        
        let netGroup = DispatchGroup()
        let workingQueue = DispatchQueue(label: "GameDetail")
        
        netGroup.enter()
        workingQueue.async {
            //HWGameDetailModel
            self.httpManager.getGameDetailTop(gameId: "584") { model in
                self.topModel = model
                
                netGroup.leave()
            } failure: { errorString in
                netGroup.leave()
            }
        }
        
        netGroup.enter()
        workingQueue.async {
            //HWGameDetailInfoModel
            self.httpManager.getGameDetailInfo(gameId: "584") { model in
                self.detailModel = model
                netGroup.leave()
            } failure: { error in
                netGroup.leave()
            }
        }
        
        netGroup.enter()
        workingQueue.async {
            self.httpManager.getGameDetailCommentList(gameId: "584") { models in
                self.commentList = models
                netGroup.leave()
            } failure: { errorString in
                netGroup.leave()
            }
        }
        
        netGroup.notify(queue: workingQueue) {
            DispatchQueue.main.asyncAfter(deadline: .now()) {
                self.topView.topModel = self.topModel
                self.bottomView.model = self.topModel
                self.detailModel.commentList = self.commentList
                self.tableView.reloadData()
            }
            
        }
    }
}

extension HWWuKongDetailListVC:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
